<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="水印文字" prop="watermarkText">
        <el-input v-model="queryParams.watermarkText" placeholder="请输入水印文字" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['archive:config:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['archive:config:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['archive:config:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['archive:config:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="configList">
      <!-- <el-table-column type="selection" width="55" align="center" />  @selection-change="handleSelectionChange"-->
      <el-table-column label="水印文字" align="center" prop="watermarkText" />
      <el-table-column label="水印字体透明度" align="center" prop="opacity" />
      <el-table-column label="水印字体大小" align="center" prop="fontsize" />
      <el-table-column label="水印倾斜角度" align="center" prop="angle" />
      <el-table-column label="竖向密度" align="center" prop="heightdensity" />
      <el-table-column label="横向密度" align="center" prop="widthdensity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button type="text" icon="Tools" @click="configuration(scope.row)">配置</el-button>
          <!-- <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:config:edit']">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['archive:config:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" /> -->

    <!-- 添加或修改水印配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="configRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="水印文字" prop="watermarkText">
          <el-input v-model="form.watermarkText" placeholder="请输入水印文字" />
        </el-form-item>
        <el-form-item label="水印字体透明度" prop="opacity">
          <el-input v-model.number="form.opacity" placeholder="请输入水印字体透明度" />
        </el-form-item>
        <el-form-item label="水印字体大小" prop="fontsize">
          <el-input v-model.number="form.fontsize" placeholder="请输入水印字体大小" />
        </el-form-item>
        <el-form-item label="水印倾斜角度" prop="angle">
          <el-input v-model.number="form.angle" placeholder="请输入水印倾斜角度" />
        </el-form-item>
        <el-form-item label="竖向密度" prop="heightdensity">
          <el-tooltip content="数值越大每页竖向水印越少" placement="bottom">
            <el-input v-model.number="form.heightdensity" placeholder="请输入竖向密度" />
          </el-tooltip>
        </el-form-item>
        <el-form-item label="横向密度" prop="widthdensity">
          <el-tooltip content="数值越大每页横向水印越少" placement="bottom">
            <el-input v-model.number="form.widthdensity" placeholder="请输入横向密度" />
          </el-tooltip>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Config">
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/base/config";

const { proxy } = getCurrentInstance();

const validateCom = (rule, value, callback) => {
  const MIN_NUMBER = 1
  const one = Number(value)
  if (Number.isInteger(one)) {
    if (one < MIN_NUMBER) {
      return callback(new Error('输入值必须为大于0的正整数'))
    }
    return callback()
  }
  return callback(new Error('请输入数字值'))
};

const configList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    watermarkText: null,
    opacity: null,
    fontsize: null,
    angle: null,
    heightdensity: null,
    widthdensity: null,
  },
  rules: {
    watermarkText: [
      { required: true, message: '水印文字不能为空', trigger: 'blur' },
    ],
    opacity: [
      { required: true, message: '水印字体透明度不能为空', trigger: 'blur' },
      { type: 'number', message: '输入只能为数字类型' },
      { validator: validateCom, trigger: 'blur' }
    ],
    fontsize: [
      { required: true, message: '水印字体大小不能为空', trigger: 'blur' },
      { type: 'number', message: '输入只能为数字类型' },
      { validator: validateCom, trigger: 'blur' }
    ],
    angle: [
      { required: true, message: '水印倾斜角度不能为空', trigger: 'blur' },
      { type: 'number', message: '输入只能为数字类型' },
      { validator: validateCom, trigger: 'blur' }
    ],
    heightdensity: [
      { required: true, message: '竖向密度不能为空', trigger: 'blur' },
      { type: 'number', message: '输入只能为数字类型' },
    ],
    widthdensity: [
      { required: true, message: '横向密度不能为空', trigger: 'blur' },
      { type: 'number', message: '输入只能为数字类型' },
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询水印配置列表 */
function getList() {
  loading.value = true;
  listConfig(queryParams.value).then(response => {
    configList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    watermarkId: null,
    watermarkText: null,
    opacity: null,
    fontsize: null,
    angle: null,
    heightdensity: null,
    widthdensity: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("configRef");
}

// /** 搜索按钮操作 */
// function handleQuery() {
//   queryParams.value.pageNum = 1;
//   getList();
// }

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// // 多选框选中数据
// function handleSelectionChange(selection) {
//   ids.value = selection.map(item => item.watermarkId);
//   single.value = selection.length != 1;
//   multiple.value = !selection.length;
// }

/** 配置按钮操作 */
function configuration(row) {
  reset();
  const watermarkId = row.watermarkId || ids.value
  getConfig(watermarkId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "水印配置";
  });
}
// /** 新增按钮操作 */
// function handleAdd() {
//   reset();
//   open.value = true;
//   title.value = "添加水印配置";
// }

// /** 修改按钮操作 */
// function handleUpdate(row) {
//   reset();
//   const watermarkId = row.watermarkId || ids.value
//   getConfig(watermarkId).then(response => {
//     form.value = response.data;
//     open.value = true;
//     title.value = "修改水印配置";
//   });
// }

/** 提交按钮 */
function submitForm() {
  proxy.$refs["configRef"].validate(valid => {
    if (valid) {
      if (form.value.watermarkId != null) {
        updateConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("配置成功");
          open.value = false;
          getList();
        });
      }
      //  else {
      //   addConfig(form.value).then(response => {
      //     proxy.$modal.msgSuccess("新增成功");
      //     open.value = false;
      //     getList();
      //   });
      // }
    }
  });
}

// /** 删除按钮操作 */
// function handleDelete(row) {
//   const watermarkIds = row.watermarkId || ids.value;
//   proxy.$modal.confirm('是否确认删除水印配置编号为"' + watermarkIds + '"的数据项？').then(function () {
//     return delConfig(watermarkIds);
//   }).then(() => {
//     getList();
//     proxy.$modal.msgSuccess("删除成功");
//   }).catch(() => { });
// }

// /** 导出按钮操作 */
// function handleExport() {
//   proxy.download('archive/config/export', {
//     ...queryParams.value
//   }, `config_${new Date().getTime()}.xlsx`)
// }

getList();
</script>
